﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Public
{
	public class ConnectDuctsByFitting
	{
		public bool IsConnecteddByRoundAngleTee(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMsg)
		{
			SubTransaction subTransaction = null;
			Document document = null;
			bool result;
			try
			{
				if (!this.IsMatchConditionsRoundAngleTee(ducts, xyzs, ref document, ref connMsg))
				{
					result = false;
				}
				else
				{
					IList<Duct> list = ducts;
					List<Alignments> list2;
					if (ducts[0].UniqueId == ducts[1].UniqueId)
					{
						list2 = this.GetAlignmentByRoundAngleTeeBeforeSperate(ducts, ref connMsg);
						list = this.GetThreeDuctsRoundAngleTee(ducts, document, xyzs, ref connMsg);
						if (list == null)
						{
							return false;
						}
					}
					else
					{
						list2 = this.GetAlignmentByRoundAngleTee(list, ref connMsg);
					}
					if (list2.Count != 2)
					{
						connMsg = "风管没有正确的对齐方式";
						result = false;
					}
					else
					{
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(list[0]);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(list[1]);
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list[2]);
						Connector closeConnector = hypiping.GetCloseConnector(hypiping3.StartConnector.Origin, false);
						Connector closeConnector2 = hypiping3.GetCloseConnector(hypiping.StartConnector.Origin, false);
						Connector closeConnector3 = hypiping2.GetCloseConnector(hypiping.StartConnector.Origin, false);
						if (closeConnector.IsConnected || closeConnector3.IsConnected || closeConnector2.IsConnected)
						{
							YJKMessageBox.Show("选择的风管已经被连接，请重新选择风管");
							result = false;
						}
						else
						{
							NineAlignments oneAlignInNineAlignments = this.GetOneAlignInNineAlignments(list2);
							string familyName = "";
							bool familyNameByRoundAngleTee = this.GetFamilyNameByRoundAngleTee(list, listName, oneAlignInNineAlignments, ref familyName);
							FamilyInstance familyInstance = this.GetFamilyInstance(document, list, familyName, familyNameByRoundAngleTee, ref connMsg);
							if (familyInstance == null)
							{
								connMsg = "没有找到相应的族，连接失败";
								result = false;
							}
							else
							{
								this.RotateElementToAlignAllDucts(familyInstance, closeConnector, closeConnector3, closeConnector2, document);
								subTransaction = new SubTransaction(document);
								subTransaction.Start();
								List<Connector> listConn = new List<Connector>
								{
									closeConnector,
									closeConnector3,
									closeConnector2
								};
								this.ModifySizeAndConnect(listConn, familyInstance);
								subTransaction.Commit();
								result = true;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
                    if (TransactionStatus.Committed != status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public bool IsConnecteddByYShapeTee(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMsg)
		{
			SubTransaction subTransaction = null;
			Document document = null;
			bool result;
			try
			{
				if (!this.IsMatchConditionsYShapeTee(ducts, xyzs, ref document, ref connMsg))
				{
					result = false;
				}
				else
				{
					IList<Duct> list = ducts;
					List<Alignments> list2;
					if (ducts[1].UniqueId == ducts[2].UniqueId)
					{
						list2 = this.GetAlignmentByRoundAngleTeeBeforeSperate(ducts, ref connMsg);
						list = this.GetThreeDuctsYShapeTee(ducts, document, xyzs, ref connMsg);
						if (list == null)
						{
							return false;
						}
					}
					else
					{
						list2 = this.GetAlignmentByYShapeTee(list, ref connMsg);
					}
					if (list2.Count != 2)
					{
						connMsg = "风管没有正确的对齐方式";
						result = false;
					}
					else
					{
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(list[0]);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(list[1]);
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list[2]);
						Connector closeConnector = hypiping.GetCloseConnector(hypiping3.StartConnector.Origin, false);
						Connector closeConnector2 = hypiping3.GetCloseConnector(hypiping.StartConnector.Origin, false);
						Connector closeConnector3 = hypiping2.GetCloseConnector(hypiping.StartConnector.Origin, false);
						if (closeConnector.IsConnected || closeConnector3.IsConnected || closeConnector2.IsConnected)
						{
							YJKMessageBox.Show("选择的风管已经被连接，请重新选择风管");
							result = false;
						}
						else
						{
							NineAlignments oneAlignInNineAlignments = this.GetOneAlignInNineAlignments(list2);
							string familyNameByYShapeTee = this.GetFamilyNameByYShapeTee(listName, oneAlignInNineAlignments);
							FamilyInstance familyInstance = this.GetFamilyInstance(document, list, familyNameByYShapeTee, false, ref connMsg);
							if (familyInstance == null)
							{
								connMsg = "没有找到相应的族，连接失败";
								result = false;
							}
							else
							{
								this.RotateElementToAlignAllDucts(familyInstance, closeConnector, closeConnector3, closeConnector2, document);
								subTransaction = new SubTransaction(document);
								subTransaction.Start();
								List<Connector> listConn = new List<Connector>
								{
									closeConnector,
									closeConnector3,
									closeConnector2
								};
								this.ModifySizeAndConnect(listConn, familyInstance);
								subTransaction.Commit();
								result = true;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
                    if (TransactionStatus.Committed != status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public bool IsConnecteddByCross(IList<Duct> ducts, IList<XYZ> xyzs, List<string> listName, ref string connMsg)
		{
			SubTransaction subTransaction = null;
			Document document = null;
			bool result;
			try
			{
				if (!this.IsMatchConditionsCross(ducts, xyzs, ref document, ref connMsg))
				{
					result = false;
				}
				else
				{
					IList<Duct> list = ducts;
					List<Alignments> list2;
					if (ducts[0].UniqueId == ducts[1].UniqueId || ducts[2].UniqueId == ducts[3].UniqueId)
					{
						list2 = this.GetAlignmentByCrossBeforeSperate(ducts, ref connMsg);
						list = this.GetFourDuctsBySperateForCorss(ducts, document, xyzs, ref connMsg);
						if (list == null)
						{
							return false;
						}
					}
					else
					{
						list2 = this.GetAlignmentByCross(list, ref connMsg);
					}
					if (list2.Count != 2)
					{
						connMsg = "风管没有正确的对齐方式";
						result = false;
					}
					else
					{
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(list[0]);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(list[1]);
						YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list[2]);
						YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(list[3]);
						Connector closeConnector = hypiping.GetCloseConnector(hypiping3.StartConnector.Origin, false);
						Connector closeConnector2 = hypiping3.GetCloseConnector(hypiping.StartConnector.Origin, false);
						Connector closeConnector3 = hypiping2.GetCloseConnector(hypiping.StartConnector.Origin, false);
						Connector closeConnector4 = hypiping4.GetCloseConnector(hypiping.StartConnector.Origin, false);
						if (closeConnector.IsConnected || closeConnector3.IsConnected || closeConnector2.IsConnected || closeConnector4.IsConnected)
						{
							YJKMessageBox.Show("选择的风管已经被连接，请重新选择风管");
							result = false;
						}
						else
						{
							NineAlignments oneAlignInNineAlignments = this.GetOneAlignInNineAlignments(list2);
							string familyNameByYShapeTee = this.GetFamilyNameByYShapeTee(listName, oneAlignInNineAlignments);
							FamilyInstance familyInstance = this.GetFamilyInstance(document, list, familyNameByYShapeTee, false, ref connMsg);
							if (familyInstance == null)
							{
								connMsg = "没有找到相应的族，连接失败";
								result = false;
							}
							else
							{
								this.RotateElementToAlignAllDucts(familyInstance, closeConnector, closeConnector3, closeConnector2, document);
								subTransaction = new SubTransaction(document);
								subTransaction.Start();
								List<Connector> listConn = new List<Connector>
								{
									closeConnector,
									closeConnector3,
									closeConnector2,
									closeConnector4
								};
								this.ModifySizeAndConnect(listConn, familyInstance);
								subTransaction.Commit();
								result = true;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
                    if (TransactionStatus.Committed != status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public void ModifySizeAndConnect(List<Connector> listConn, FamilyInstance fi)
		{
			int num = 0;
			foreach (Connector connector in listConn)
			{
				Connector connectorByTheSameVector = this.GetConnectorByTheSameVector(fi, -connector.CoordinateSystem.BasisZ);
				connectorByTheSameVector.Width = connector.Width;
				connectorByTheSameVector.Height = connector.Height;
				if (num == 0)
				{
					XYZ xyz = new YJKLine(Line.CreateUnbound(connector.Origin, connector.CoordinateSystem.BasisZ)).GetProject(connectorByTheSameVector.Origin) - connectorByTheSameVector.Origin;
					ElementTransformUtils.MoveElement(fi.Document, fi.Id, xyz);
					num++;
				}
				connector.ConnectTo(connectorByTheSameVector);
			}
		}

		public Connector GetConnectorByTheSameVector(FamilyInstance fi, XYZ vector)
		{
			Connector result = null;
			if (fi != null)
			{
				ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(vector))
					{
						result = connector;
					}
				}
			}
			return result;
		}

		private bool IsMatchConditionsRoundAngleTee(IList<Duct> ducts, IList<XYZ> xyzs, ref Document doc, ref string connMsg)
		{
			if (ducts.Count != 3 || xyzs.Count != 3)
			{
				connMsg = "三通连接必须是三条风管！";
				return false;
			}
			doc = ducts[0].Document;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
			if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				connMsg = "同一根风管不能构成三通连接！";
				return false;
			}
			if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				connMsg = "第一根风管与第三根风管不能为同一风管！";
				return false;
			}
			if (hypiping2.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				connMsg = "第二根风管与第三根风管不能为同一风管！";
				return false;
			}
			if (!hypiping.IsVertical(ducts[2]))
			{
				connMsg = "第一根风管与第三根风管必须垂直！";
				return false;
			}
			if (!hypiping.IsParallel(ducts[1]))
			{
				connMsg = "第一根风管与第二根风管必须平行！";
				return false;
			}
			return true;
		}

		private bool IsMatchConditionsYShapeTee(IList<Duct> ducts, IList<XYZ> xyzs, ref Document doc, ref string connMsg)
		{
			if (ducts.Count != 3 || xyzs.Count != 3)
			{
				connMsg = "三通连接必须是三条风管！";
				return false;
			}
			doc = ducts[0].Document;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
			if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				connMsg = "同一根风管不能构成三通连接！";
				return false;
			}
			if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				connMsg = "第一根风管与第三根风管不能为同一风管！";
				return false;
			}
			if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id))
			{
				connMsg = "第一根风管与第二根风管不能为同一风管！";
				return false;
			}
			if (!hypiping.IsVertical(ducts[2]))
			{
				connMsg = "第一根风管与第三根风管必须垂直！";
				return false;
			}
			if (!hypiping.IsVertical(ducts[1]))
			{
				connMsg = "第一根风管与第二根风管必须垂直！";
				return false;
			}
			return true;
		}

		private bool IsMatchConditionsCross(IList<Duct> ducts, IList<XYZ> xyzs, ref Document doc, ref string connMsg)
		{
			if (ducts.Count != 4 || xyzs.Count != 4)
			{
				connMsg = "四通连接必须是四条风管！";
				return false;
			}
			doc = ducts[0].Document;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], xyzs[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], xyzs[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], xyzs[2]);
			YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[3], xyzs[3]);
			if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id) && hypiping.Piping.Id.Equals(hypiping4.Piping.Id))
			{
				connMsg = "同一根风管不能构成四通连接！";
				return false;
			}
			if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				connMsg = "第一根风管与第三根风管不能为同一风管！";
				return false;
			}
			if (hypiping.Piping.Id.Equals(hypiping4.Piping.Id))
			{
				connMsg = "第一根风管与第四根风管不能为同一风管！";
				return false;
			}
			if (hypiping2.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				connMsg = "第二根风管与第三根风管不能为同一风管！";
				return false;
			}
			if (hypiping2.Piping.Id.Equals(hypiping4.Piping.Id))
			{
				connMsg = "第二根风管与第四根风管不能为同一风管！";
				return false;
			}
			if (!hypiping.IsVertical(ducts[2]))
			{
				connMsg = "第一根风管与第三根风管必须垂直！";
				return false;
			}
			if (!hypiping.IsVertical(ducts[3]))
			{
				connMsg = "第一根风管与第四根风管必须垂直！";
				return false;
			}
			if (!hypiping.IsParallel(ducts[1]))
			{
				connMsg = "第一根风管与第二根风管必须平行！";
				return false;
			}
			if (!hypiping3.IsParallel(ducts[3]))
			{
				connMsg = "第三根风管与第四根风管必须平行！";
				return false;
			}
			return true;
		}

		private IList<Duct> GetThreeDuctsRoundAngleTee(IList<Duct> ducts, Document doc, IList<XYZ> xyzs, ref string connMsg)
		{
			IList<Duct> ductsBySperateDuct = this.GetDuctsBySperateDuct(ducts[0], ducts[2], xyzs[0], doc, ref connMsg);
			if (ductsBySperateDuct == null)
			{
				return null;
			}
			return new List<Duct>
			{
				ductsBySperateDuct[0],
				ductsBySperateDuct[1],
				ducts[2]
			};
		}

		private IList<Duct> GetThreeDuctsYShapeTee(IList<Duct> ducts, Document doc, IList<XYZ> xyzs, ref string connMsg)
		{
			IList<Duct> ductsBySperateDuct = this.GetDuctsBySperateDuct(ducts[2], ducts[0], xyzs[1], doc, ref connMsg);
			if (ductsBySperateDuct == null)
			{
				return null;
			}
			return new List<Duct>
			{
				ducts[0],
				ductsBySperateDuct[0],
				ductsBySperateDuct[1]
			};
		}

		private IList<Duct> GetFourDuctsBySperateForCorss(IList<Duct> ducts, Document doc, IList<XYZ> xyzs, ref string connMsg)
		{
			if (ducts[0].UniqueId == ducts[1].UniqueId && ducts[2].UniqueId == ducts[3].UniqueId)
			{
				IList<Duct> ductsBySperateDuct = this.GetDuctsBySperateDuct(ducts[0], ducts[3], xyzs[0], doc, ref connMsg);
				if (ductsBySperateDuct == null)
				{
					return null;
				}
				IList<Duct> ductsBySperateDuct2 = this.GetDuctsBySperateDuct(ducts[3], ductsBySperateDuct[0], xyzs[2], doc, ref connMsg);
				if (ductsBySperateDuct2 == null)
				{
					return null;
				}
				return new List<Duct>
				{
					ductsBySperateDuct[0],
					ductsBySperateDuct[1],
					ductsBySperateDuct2[0],
					ductsBySperateDuct2[1]
				};
			}
			else if (ducts[0].UniqueId == ducts[1].UniqueId && ducts[2].UniqueId != ducts[3].UniqueId)
			{
				IList<Duct> ductsBySperateDuct3 = this.GetDuctsBySperateDuct(ducts[0], ducts[3], xyzs[0], doc, ref connMsg);
				if (ductsBySperateDuct3 == null)
				{
					return null;
				}
				return new List<Duct>
				{
					ductsBySperateDuct3[0],
					ductsBySperateDuct3[1],
					ducts[2],
					ducts[3]
				};
			}
			else
			{
				if (!(ducts[0].UniqueId != ducts[1].UniqueId) || !(ducts[2].UniqueId == ducts[3].UniqueId))
				{
					return null;
				}
				IList<Duct> ductsBySperateDuct4 = this.GetDuctsBySperateDuct(ducts[3], ducts[0], xyzs[2], doc, ref connMsg);
				if (ductsBySperateDuct4 == null)
				{
					return null;
				}
				return new List<Duct>
				{
					ducts[0],
					ducts[1],
					ductsBySperateDuct4[0],
					ductsBySperateDuct4[1]
				};
			}
		}

		private IList<Duct> GetDuctsBySperateDuct(Duct speratedDuct, Duct otherDuct, XYZ xyz, Document doc, ref string connMsg)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(speratedDuct);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(otherDuct);
			XYZ dlineP = YJKRevitTools.GetDLineP(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin, hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			if (dlineP == null)
			{
				connMsg = "第一根管和第三根管没有交点";
				return null;
			}
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			IList<Duct> list = new List<Duct>();
			list = hypiping.Separate(dlineP);
			hyrotateFamilyInstance.MovePiping(list[0]);
			hyrotateFamilyInstance.MovePiping(list[1]);
			return this.GetOrderDucts(list, xyz);
		}

		private List<Alignments> GetAlignmentByRoundAngleTee(IList<Duct> ducts, ref string connMsg)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
			Connector closeConnector = hypiping.GetCloseConnector(hypiping2.StartConnector.Origin, false);
			DetermineTwoDuctsAlignment determineTwoDuctsAlignment = new DetermineTwoDuctsAlignment(closeConnector.CoordinateSystem.BasisZ, ducts[0], ducts[1]);
			List<Alignments> verticalAlignmentsByTwoDucts = determineTwoDuctsAlignment.GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts.Count <= 0)
			{
				connMsg = "第一根管和第二根管没有符合的对齐方式";
				return null;
			}
			List<Alignments> verticalAlignmentsByTwoDucts2 = new DetermineTwoDuctsAlignment(closeConnector.CoordinateSystem.BasisZ, ducts[0], ducts[2]).GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts2.Count <= 0)
			{
				connMsg = "第一根管和第三根管没有符合的对齐方式";
				return null;
			}
			List<Alignments> list = verticalAlignmentsByTwoDucts.Intersect(verticalAlignmentsByTwoDucts2).ToList<Alignments>();
			if (list.Count <= 0)
			{
				connMsg = "三根管没有共同的对齐方式";
				return null;
			}
			if (list.Count > 1)
			{
				list.Clear();
				list.Add(Alignments.VerticalMiddle);
			}
			List<Alignments> horizontalAlignmentsByTwoDucts = determineTwoDuctsAlignment.GetHorizontalAlignmentsByTwoDucts();
			if (horizontalAlignmentsByTwoDucts.Count <= 0)
			{
				connMsg = "第一根管和第二根管没有符合的对齐方式";
				return null;
			}
			if (horizontalAlignmentsByTwoDucts.Count > 1)
			{
				horizontalAlignmentsByTwoDucts.Clear();
				horizontalAlignmentsByTwoDucts.Add(Alignments.HorizontalMiddle);
			}
			return list.Union(horizontalAlignmentsByTwoDucts).ToList<Alignments>();
		}

		private List<Alignments> GetAlignmentByYShapeTee(IList<Duct> ducts, ref string connMsg)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
			Connector closeConnector = hypiping.GetCloseConnector(hypiping2.StartConnector.Origin, false);
			List<Alignments> verticalAlignmentsByTwoDucts = new DetermineTwoDuctsAlignment(closeConnector.CoordinateSystem.BasisZ, ducts[0], ducts[1]).GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts.Count <= 0)
			{
				connMsg = "第一根管和第二根管没有符合的对齐方式";
				return null;
			}
			DetermineTwoDuctsAlignment determineTwoDuctsAlignment = new DetermineTwoDuctsAlignment(-closeConnector.CoordinateSystem.BasisX, ducts[1], ducts[2]);
			List<Alignments> verticalAlignmentsByTwoDucts2 = determineTwoDuctsAlignment.GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts2.Count <= 0)
			{
				connMsg = "第二根管和第三根管没有符合的对齐方式";
				return null;
			}
			List<Alignments> list = verticalAlignmentsByTwoDucts.Intersect(verticalAlignmentsByTwoDucts2).ToList<Alignments>();
			if (list.Count <= 0)
			{
				connMsg = "三根管没有共同的对齐方式";
				return null;
			}
			if (list.Count > 1)
			{
				list.Clear();
				list.Add(Alignments.VerticalMiddle);
			}
			List<Alignments> horizontalAlignmentsByTwoDucts = determineTwoDuctsAlignment.GetHorizontalAlignmentsByTwoDucts();
			if (horizontalAlignmentsByTwoDucts.Count <= 0)
			{
				connMsg = "第一根管和第二根管没有符合的对齐方式";
				return null;
			}
			if (horizontalAlignmentsByTwoDucts.Count > 1)
			{
				horizontalAlignmentsByTwoDucts.Clear();
				horizontalAlignmentsByTwoDucts.Add(Alignments.HorizontalMiddle);
			}
			return list.Union(horizontalAlignmentsByTwoDucts).ToList<Alignments>();
		}

		private List<Alignments> GetAlignmentByRoundAngleTeeBeforeSperate(IList<Duct> ducts, ref string connMsg)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[2]);
			List<Alignments> verticalAlignmentsByTwoDucts = new DetermineTwoDuctsAlignment(hypiping.GetCloseConnector(hypiping2.StartConnector.Origin, false).CoordinateSystem.BasisZ, ducts[0], ducts[2]).GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts.Count <= 0)
			{
				connMsg = "第一根管和第三根管没有符合的对齐方式";
				return null;
			}
			if (verticalAlignmentsByTwoDucts.Count > 1)
			{
				verticalAlignmentsByTwoDucts.Clear();
				verticalAlignmentsByTwoDucts.Add(Alignments.VerticalMiddle);
			}
			List<Alignments> second = new List<Alignments>
			{
				Alignments.HorizontalMiddle
			};
			return verticalAlignmentsByTwoDucts.Union(second).ToList<Alignments>();
		}

		private List<Alignments> GetAlignmentByCrossBeforeSperate(IList<Duct> ducts, ref string connMsg)
		{
			if (ducts[0].UniqueId == ducts[1].UniqueId && ducts[2].UniqueId == ducts[3].UniqueId)
			{
				List<Duct> ducts2 = new List<Duct>
				{
					ducts[0],
					ducts[1],
					ducts[3]
				};
				return this.GetAlignmentByRoundAngleTeeBeforeSperate(ducts2, ref connMsg);
			}
			if (ducts[0].UniqueId == ducts[1].UniqueId && ducts[2].UniqueId != ducts[3].UniqueId)
			{
				List<Duct> ducts3 = new List<Duct>
				{
					ducts[0],
					ducts[2],
					ducts[3]
				};
				return this.GetAlignmentByYShapeTee(ducts3, ref connMsg);
			}
			if (ducts[0].UniqueId != ducts[1].UniqueId && ducts[2].UniqueId == ducts[3].UniqueId)
			{
				List<Duct> ducts4 = new List<Duct>
				{
					ducts[0],
					ducts[1],
					ducts[3]
				};
				return this.GetAlignmentByRoundAngleTee(ducts4, ref connMsg);
			}
			return null;
		}

		private List<Alignments> GetAlignmentByCross(IList<Duct> ducts, ref string connMsg)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
			Connector closeConnector = hypiping.GetCloseConnector(hypiping2.StartConnector.Origin, false);
			DetermineTwoDuctsAlignment determineTwoDuctsAlignment = new DetermineTwoDuctsAlignment(closeConnector.CoordinateSystem.BasisZ, ducts[0], ducts[1]);
			List<Alignments> verticalAlignmentsByTwoDucts = determineTwoDuctsAlignment.GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts.Count <= 0)
			{
				connMsg = "第一根管和第二根管没有符合的对齐方式";
				return null;
			}
			List<Alignments> verticalAlignmentsByTwoDucts2 = new DetermineTwoDuctsAlignment(closeConnector.CoordinateSystem.BasisZ, ducts[0], ducts[2]).GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts2.Count <= 0)
			{
				connMsg = "第一根管和第三根管没有符合的对齐方式";
				return null;
			}
			List<Alignments> list = verticalAlignmentsByTwoDucts.Intersect(verticalAlignmentsByTwoDucts2).ToList<Alignments>();
			if (list.Count <= 0)
			{
				connMsg = "四根管没有共同的对齐方式";
				return null;
			}
			DetermineTwoDuctsAlignment determineTwoDuctsAlignment2 = new DetermineTwoDuctsAlignment(-closeConnector.CoordinateSystem.BasisX, ducts[2], ducts[3]);
			List<Alignments> verticalAlignmentsByTwoDucts3 = determineTwoDuctsAlignment2.GetVerticalAlignmentsByTwoDucts();
			if (verticalAlignmentsByTwoDucts3.Count <= 0)
			{
				connMsg = "第三根管和第四根管没有符合的对齐方式";
				return null;
			}
			list = list.Intersect(verticalAlignmentsByTwoDucts3).ToList<Alignments>();
			if (list.Count <= 0)
			{
				connMsg = "四根管没有共同的对齐方式";
				return null;
			}
			if (list.Count > 1)
			{
				list.Clear();
				list.Add(Alignments.VerticalMiddle);
			}
			List<Alignments> list2 = determineTwoDuctsAlignment.GetHorizontalAlignmentsByTwoDucts();
			if (list2.Count <= 0)
			{
				connMsg = "第一根管和第二根管没有符合的对齐方式";
				return null;
			}
			List<Alignments> horizontalAlignmentsByTwoDucts = determineTwoDuctsAlignment2.GetHorizontalAlignmentsByTwoDucts();
			if (horizontalAlignmentsByTwoDucts.Count <= 0)
			{
				connMsg = "第三根管和第四根管没有符合的对齐方式";
				return null;
			}
			list2 = list2.Intersect(horizontalAlignmentsByTwoDucts).ToList<Alignments>();
			if (list2.Count <= 0)
			{
				connMsg = "四根管没有符合的对齐方式";
				return null;
			}
			if (list2.Count > 1)
			{
				list2.Clear();
				list2.Add(Alignments.HorizontalMiddle);
			}
			return list.Union(list2).ToList<Alignments>();
		}

		private NineAlignments GetOneAlignInNineAlignments(List<Alignments> list)
		{
			if (list.Count != 2)
			{
				return NineAlignments.None;
			}
			Alignments alignments = list[0];
			Alignments alignments2 = list[1];
			if (alignments == Alignments.Top)
			{
				if (alignments2 == Alignments.Left)
				{
					return NineAlignments.TopLeft;
				}
				if (alignments2 == Alignments.Right)
				{
					return NineAlignments.TopRight;
				}
				if (alignments2 == Alignments.HorizontalMiddle)
				{
					return NineAlignments.TopMiddle;
				}
			}
			else if (alignments == Alignments.Bottom)
			{
				if (alignments2 == Alignments.Left)
				{
					return NineAlignments.BottomLeft;
				}
				if (alignments2 == Alignments.Right)
				{
					return NineAlignments.BottomRight;
				}
				if (alignments2 == Alignments.HorizontalMiddle)
				{
					return NineAlignments.BottomMiddle;
				}
			}
			else if (alignments == Alignments.VerticalMiddle)
			{
				if (alignments2 == Alignments.Left)
				{
					return NineAlignments.MiddleLeft;
				}
				if (alignments2 == Alignments.Right)
				{
					return NineAlignments.MiddleRight;
				}
				if (alignments2 == Alignments.HorizontalMiddle)
				{
					return NineAlignments.MiddleMiddle;
				}
			}
			return NineAlignments.None;
		}

		private bool GetFamilyNameByRoundAngleTee(IList<Duct> ducts, List<string> listName, NineAlignments na, ref string familyName)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[2]);
			Connector closeConnector = hypiping.GetCloseConnector(hypiping2.StartConnector.Origin, false);
			bool flag = hypiping2.GetCloseConnector(hypiping.StartConnector.Origin, false).CoordinateSystem.BasisZ.IsAlmostEqualTo(closeConnector.CoordinateSystem.BasisX);
			switch (na)
			{
			case NineAlignments.TopLeft:
				if (flag)
				{
					familyName = listName[1];
					return true;
				}
				familyName = listName[0];
				return false;
			case NineAlignments.TopRight:
				if (flag)
				{
					familyName = listName[0];
					return true;
				}
				familyName = listName[1];
				return false;
			case NineAlignments.TopMiddle:
				familyName = listName[2];
				return flag;
			case NineAlignments.BottomLeft:
				if (flag)
				{
					familyName = listName[4];
					return true;
				}
				familyName = listName[3];
				return false;
			case NineAlignments.BottomRight:
				if (flag)
				{
					familyName = listName[3];
					return true;
				}
				familyName = listName[4];
				return false;
			case NineAlignments.BottomMiddle:
				familyName = listName[5];
				return flag;
			case NineAlignments.MiddleLeft:
				if (flag)
				{
					familyName = listName[7];
					return true;
				}
				familyName = listName[6];
				return false;
			case NineAlignments.MiddleRight:
				if (flag)
				{
					familyName = listName[6];
					return true;
				}
				familyName = listName[7];
				return false;
			case NineAlignments.MiddleMiddle:
				familyName = listName[8];
				return flag;
			default:
				return false;
			}
		}

		private string GetFamilyNameByYShapeTee(List<string> listName, NineAlignments na)
		{
			string result = "";
			switch (na)
			{
			case NineAlignments.TopLeft:
				result = listName[0];
				break;
			case NineAlignments.TopRight:
				result = listName[1];
				break;
			case NineAlignments.TopMiddle:
				result = listName[2];
				break;
			case NineAlignments.BottomLeft:
				result = listName[3];
				break;
			case NineAlignments.BottomRight:
				result = listName[4];
				break;
			case NineAlignments.BottomMiddle:
				result = listName[5];
				break;
			case NineAlignments.MiddleLeft:
				result = listName[6];
				break;
			case NineAlignments.MiddleRight:
				result = listName[7];
				break;
			case NineAlignments.MiddleMiddle:
				result = listName[8];
				break;
			}
			return result;
		}

		private FamilyInstance GetFamilyInstance(Document doc, IList<Duct> ducts, string familyName, bool isMirror, ref string connMsg)
		{
			string text = "";
			FamilySymbol familySymbol = this.GetFamilySymbol(familyName, doc, ref text);
			if (familySymbol != null)
			{
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[2]);
				Connector closeConnector = hypiping.GetCloseConnector(hypiping2.StartConnector.Origin, false);
				SubTransaction subTransaction = new SubTransaction(doc);
				subTransaction.Start();
				UnifiedModified.ActivateFamilySymbol(familySymbol);
				FamilyInstance familyInstance = doc.Create.NewFamilyInstance(closeConnector.Origin, familySymbol, 0);
				doc.Regenerate();
				if (isMirror)
				{
					familyInstance = this.GetMirrorTee(familyInstance);
				}
				subTransaction.Commit();
				return familyInstance;
			}
			if (!string.IsNullOrEmpty(text))
			{
				connMsg = text;
				return null;
			}
			connMsg = "加载族失败！";
			return null;
		}

		public FamilyInstance GetMirrorTee(FamilyInstance teeFi)
		{
			Document document = teeFi.Document;
			HYSystemElementFilter hysystemElementFilter = new HYSystemElementFilter(document);
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			IList<Element> allDuctFitting = hysystemElementFilter.GetAllDuctFitting();
			Connector teeMainConnector = new ConnectHelper().GetTeeMainConnector(teeFi);
			hyrotateFamilyInstance.MirrorFamilyInstance(teeFi, teeMainConnector.CoordinateSystem.BasisZ, teeMainConnector.CoordinateSystem.BasisY, teeMainConnector.Origin);
			document.Regenerate();
			IList<Element> allDuctFitting2 = hysystemElementFilter.GetAllDuctFitting();
			FamilyInstance result = hysystemElementFilter.GetNewDuctFitting(allDuctFitting, allDuctFitting2) as FamilyInstance;
			document.DeleteElement(teeFi);
			return result;
		}

		public void RotateElementByBaseVactor(Connector connDuct, Element ele, Connector connTee)
		{
			XYZ xyz = -connDuct.CoordinateSystem.BasisZ;
			XYZ basisZ = connTee.CoordinateSystem.BasisZ;
			XYZ xyz2 = basisZ.CrossProduct(xyz).Normalize();
			if (xyz2.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0)))
			{
				xyz2 = new XYZ(0.0, 0.0, 1.0);
			}
			double angleOnPlaneTo = YJKRevitTools.GetAngleOnPlaneTo(basisZ, xyz, xyz2);
			XYZ point = ((LocationPoint)ele.Location).Point;
			Line line = Line.CreateBound(point, point.Add(xyz2));
			ElementTransformUtils.RotateElement(ele.Document, ele.Id, line, angleOnPlaneTo);
			new YJKRotateFamilyInstance().MoveFamilyInstance(ele as FamilyInstance, new XYZ(0.0, 1.0, 0.0));
			connTee = new ConnectHelper().GetTeeOutConnector(ele as FamilyInstance, -xyz);
			YJKLine hyline = new YJKLine(Line.CreateUnbound(connDuct.Origin, -xyz));
			if (!hyline.IsPointOnLine(connTee.Origin))
			{
				XYZ xyz3 = hyline.GetProject(connTee.Origin) - connTee.Origin;
				ElementTransformUtils.MoveElement(ele.Document, ele.Id, xyz3);
			}
		}

		public void RotateElementToAlignAllDucts(FamilyInstance teeFi, Connector connA, Connector connB, Connector connC, Document doc)
		{
			ConnectHelper connectHelper = new ConnectHelper();
			Connector teeMainConnector = connectHelper.GetTeeMainConnector(teeFi);
			this.RotateElementByBaseVactor(connA, teeFi, teeMainConnector);
			Connector connector = connectHelper.GetTeeOutConnector(teeFi, connC.CoordinateSystem.BasisZ);
			if (connector == null)
			{
				connector = this.RotateFamilyInstance(teeFi, connC, connectHelper, teeMainConnector);
			}
			if (connector != null)
			{
				YJKLine hyline = new YJKLine(Line.CreateUnbound(connC.Origin, connC.CoordinateSystem.BasisZ));
				if (!hyline.IsPointOnLine(connector.Origin))
				{
					XYZ xyz = hyline.GetProject(connector.Origin) - connector.Origin;
					ElementTransformUtils.MoveElement(doc, teeFi.Id, xyz);
				}
			}
		}

		private Connector RotateFamilyInstance(FamilyInstance teeFi, Connector connC, ConnectHelper cm, Connector teeMainConn)
		{
			XYZ basisZ = teeMainConn.CoordinateSystem.BasisZ;
			Connector teeSideConnector = cm.GetTeeSideConnector(teeFi, basisZ);
			XYZ basisZ2 = teeSideConnector.CoordinateSystem.BasisZ;
			XYZ vector = connC.CoordinateSystem.BasisZ * -1.0;
			double twoVectorAngle = YJKRevitTools.GetTwoVectorAngle(basisZ2, vector, basisZ);
			new YJKRotateFamilyInstance().RotateElem(teeFi, teeMainConn.Origin, basisZ, twoVectorAngle * 180.0 / Math.PI);
			return teeSideConnector;
		}

		public FamilySymbol GetFamilySymbol(string fsName, Document doc, ref string loadMsg)
		{
			FamilySymbol familySymbol = null;
			if (string.IsNullOrEmpty(fsName))
			{
				loadMsg = "族名不能为空！";
				return familySymbol;
			}
			familySymbol = YJKRevitTools.GetFamilySymbol(fsName, doc);
			if (familySymbol == null)
			{
				string text = Path.Combine(Product.FamilyLocation, RevitVersion.Version, "Fitting", fsName + ".rfa");
				if (!File.Exists(text))
				{
					loadMsg = "族不存在或者加载地址不正确！";
					return familySymbol;
				}
				SubTransaction subTransaction = new SubTransaction(doc);
				subTransaction.Start();
				Family family = null;
				doc.LoadFamily(text, out family);
				if (family == null)
				{
					subTransaction.RollBack();
					return null;
				}
				familySymbol = YJKRevitTools.GetFamilySymbol(fsName, doc);
				subTransaction.Commit();
			}
			return familySymbol;
		}

		private IList<Duct> GetOrderDucts(IList<Duct> listDuct, XYZ pickPoint)
		{
			IList<Duct> list = new List<Duct>();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			double num = hypiping.GetProject(pickPoint).DistanceTo(pickPoint);
			double num2 = hypiping2.GetProject(pickPoint).DistanceTo(pickPoint);
			if (num > num2)
			{
				list.Add(listDuct[1]);
				list.Add(listDuct[0]);
			}
			else
			{
				list.Add(listDuct[0]);
				list.Add(listDuct[1]);
			}
			return list;
		}
	}
}
